package com.onluy.framework.base;
  
 

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.onluy.framework.data.util.InParam;
import com.onluy.framework.system.RespInfo;
import com.onluy.framework.tools.utils.ParamTool;
import  RespEncrypt;   
public abstract   class AppController    {
	 protected Logger log = LoggerFactory.getLogger(this.getClass()); 
	 		 
			@Autowired
			@Resource(name = "htmlContextInfo")
			public HtmlContextInfo webinfo; 
			@Autowired
			@Resource(name = "modelAndView")
			public ModelAndView modelAndView; 
			@Autowired
			@Resource(name = "jsonResult")
			public JsonResult jsonResult; 
			@Autowired
		    protected HttpServletRequest request;
 
			public abstract AppService getAppSvc(); 
	 
			/**
			 * 鍘熷鍝嶅簲respInfo鍔犲瘑杩斿洖缁檃pp绔�
			 * @param respInfo
			 * @return
			 */
			protected RespEncrypt encryptResp(RespInfo respInfo){
				RespEncrypt respEncrypt = new RespEncrypt(); 
				//鍘熸湰杩斿洖缁撴灉鏄槑鏂囩殑锛寀bssapp闇�瑕佸姞瀵嗗悗鍐嶈繑鍥炵粰app
				String newRespInfo = ParamTool.transRespInfo(respInfo); //鍔犲瘑鍚庣殑瀵嗘枃鍝嶅簲
				respEncrypt.setRespInfo(newRespInfo); //鍖呰涓�
				return respEncrypt;
			} 
			
			@RequestMapping("/index")
		    public ModelAndView index(InParam<String, Object> inParam){ 
				log.info("---inParam:"+inParam.size()); 
		        webinfo.setBasePath(request.getContextPath());
		        //webinfo.setTitle("------------");
		        modelAndView.addObject("webinfo",webinfo);//閫氱敤鍓嶅彴瀵硅薄 
		        return modelAndView;
		    } 
			

		public JsonResult Add(InParam<String, Object> inParam) {
			log.info("Add inParam : "+inParam.toString());
			int num=1; 
			try {
				num=getAppSvc().add( inParam);
			} catch (Exception e) { 
				jsonResult.setMsg("澧炲姞澶辫触:"+e.getMessage());
			}
			if(  num>0 ){ 
				jsonResult.setSuccess(true);
				jsonResult.setMsg("澧炲姞鎴愬姛锛�");
			} 
			else {
				jsonResult.setMsg("澧炲姞澶辫触锛�"); 
			}
			return jsonResult;
		}
		
		public JsonResult Update(InParam<String, Object> inParam) {  
			log.info("Update inParam : "+inParam.toString());
			int num=1; 
			try {
				num=getAppSvc().update( inParam);
			} catch (Exception e) { 
				jsonResult.setMsg("淇敼澶辫触:"+e.getMessage());
			}
			if(  num>0 ){ 
				jsonResult.setSuccess(true);
				jsonResult.setMsg("淇敼鎴愬姛锛�");
			}
			else {
				jsonResult.setMsg("淇敼澶辫触锛�"); 
			}
			return jsonResult;
		}
	 

		public JsonResult Del(InParam<String, Object> inParam) { 
			log.info("Del inParam : "+inParam.toString()); 
			int num=1; 
			try {
				num=getAppSvc().del(  inParam);
			} catch (Exception e) { 
				jsonResult.setMsg("鍒犻櫎澶辫触:"+e.getMessage());
			}
			if(  num>0 ){ 
				jsonResult.setSuccess(true);
				jsonResult.setMsg("鍒犻櫎鎴愬姛锛�");
			} 
			else {
				jsonResult.setMsg("鍒犻櫎澶辫触锛�"); 
			}
			return jsonResult;
		}
	 
		public RespEncrypt  List(InParam<String, Object> inParam) {  
			log.info("List inParam : "+inParam.toString()); 
			java.util.List<InParam<String, Object>>  list =getAppSvc().listByPage(inParam);  
			jsonResult.setSuccess(true); 
			jsonResult.rows =list;
			jsonResult.total=inParam.getInt("total"); //鍒嗛〉鎺т欢宸茬粡鏌ヨ鍑烘�绘暟
			
			RespEncrypt respEncrypt = new RespEncrypt();
			String newRespInfo = ParamTool.transRespInfo(JSON.toJSONString(jsonResult)); //鍔犲瘑鍚庣殑瀵嗘枃鍝嶅簲
			respEncrypt.setRespInfo(newRespInfo); //鍖呰涓�
			
			return respEncrypt;
		} 
		
		

		public JsonResult  simplelist(InParam<String, Object> inParam) {  
			java.util.List<InParam<String, Object>>  list =getAppSvc().simplelist(inParam); 
			jsonResult.setSuccess(true); 
			jsonResult.rows =list; 
			return jsonResult;
		} 
 
}
